package com.ruoyi.use.aspect;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.use.domain.ExaminationUse;
import com.ruoyi.use.service.IExaminationUseService;
import com.ruoyi.examination.service.IExaminationService;
import com.ruoyi.examination.vo.ExaminationVO;
import com.ruoyi.storeroomuse.service.IStoreroomUseService;
import com.ruoyi.storeroomuse.vo.StoreUseVO;
import com.ruoyi.system.service.IStoreroomService;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

// 出库
@Aspect
@Component
public class ExamOutboundAspect {

    @Autowired
    IExaminationUseService examinationUseService;

    @Autowired
    IExaminationService examinationService;

    @Autowired
    IStoreroomUseService storeroomUseService;

    @Autowired
    IStoreroomService storeroomService;

    @Pointcut("execution(public * com.ruoyi.examination.service.IExaminationService.outbound(..))")
    public void myMethod(){
        //代理方法
    }
    @After("myMethod()")
    public void afterMethod(JoinPoint joinPoint){
        Object[] args = joinPoint.getArgs();
        for (Object every : args) {
            if (every instanceof String){
                String eve=(String) every;
                String[] strings = Convert.toStrArray(eve);
                for (String str: strings) {
                    // 添加使用记录列表
                    ExaminationUse examinationUse = new ExaminationUse();
                    ExaminationVO examinationVO = examinationService.selectExaminationByExaminationId(Long.parseLong(str));
                    examinationUse.setExaminationId(examinationVO.getExaminationId());
                    SysUser sysUser = ShiroUtils.getSysUser();
                    examinationUse.setFounder(sysUser.getUserId());
                    examinationUse.setOperationName(3L);
                    examinationUseService.insertExaminationUse(examinationUse);
                    // 添加库房列表
                    StoreUseVO storeUseVO = new StoreUseVO();
                    storeUseVO.setStoreroomId(examinationVO.getStoreroomId());
                    storeUseVO.setExaminationId(examinationVO.getExaminationId());
                    storeUseVO.setFounder(sysUser.getUserId());
                    storeUseVO.setOperationName(4L);
                    storeroomUseService.insertStoreroomUse(storeUseVO);
                }
            }
        }
    }
}
